package com.uduemc.biso.master.backend.validator.impl;

import java.io.IOException;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.CollectionUtils;

import com.uduemc.biso.core.extities.center.SysLanguage;
import com.uduemc.biso.master.backend.service.HostService;
import com.uduemc.biso.master.backend.validator.SiteLanguageIdsExist;
import com.uduemc.biso.master.config.SpringContextUtils;

public class SiteLanguageIdsExistValid implements ConstraintValidator<SiteLanguageIdsExist, List<Long>> {

	@Override
	public void initialize(SiteLanguageIdsExist constraintAnnotation) {

	}

	@Override
	public boolean isValid(List<Long> value, ConstraintValidatorContext context) {
		if (CollectionUtils.isEmpty(value)) {
			return false;
		}
		HostService hostServiceImpl = SpringContextUtils.getBean("hostServiceImpl", HostService.class);

		List<SysLanguage> sysLanguage = null;
		try {
			sysLanguage = hostServiceImpl.getSysLanguage();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!CollectionUtils.isEmpty(sysLanguage)) {
			for (Long id : value) {
				if (id == null) {
					return false;
				}
				boolean bool = false;
				for (SysLanguage language : sysLanguage) {
					if (language.getId() != null && language.getId().longValue() == id.longValue()) {
						bool = true;
						break;
					}
				}
				if (!bool) {
					return false;
				}
			}
		}
		return true;
	}

}
